package com.jbm.jbmsupplier.zp.model;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.jbm.core.client.MainCallback;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.MainActivity;
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.onehome.common.OHInterface;

/**
 * 这是MyService 的Model
 * Created by JBMhaier on 2017/5/2.
 */

public class MyServiceModel {
    private final SharedPreferActivity sharedLoginActivity;
    private Context context;
    MainCallback callback;
    /**
     * 正式服务器
     */
    /**/
     private  String mServerIp = "app.jiabangmang.com";
     private  int mSockPort = 1920;
     private  String mConnType = "app_mobile";
    /**
     * 测试服务器
     */
//    private static String mServerIp = "192.168.0.178";
//    private static String mServerIp = "192.168.0.205";
//      private static String mServerIp = "192.168.0.16";
//    private static int mSockPort = 1920;
//    private static String mConnType = "app_mobile";
//    private static String mConnType = "app_type";
    private String name;
    public MyServiceModel(Context context,MainCallback callback,String name){
        this.context=context;
        this.callback=callback;
        this.name=name;
        sharedLoginActivity=new SharedPreferActivity(context);
        if(sharedLoginActivity.getBoolearNetConn()==false){
        init();
        }else{
            if (name.equals("关机")){
                init() ;
            }else if (name.equals("开关")){
                connboo=true;
                Intent in=   new Intent();
                in.setAction("SOCKCONN");
                in.putExtra("comm","3");
                context.sendBroadcast(in);
            }else{
                connboo=true;
                Intent in=   new Intent();
                in.setAction("SOCKCONN");
                in.putExtra("comm","3");
                context.sendBroadcast(in);
            }
        }

    }
    private void init() {
        new Thread(SocketConnet).start();
    }
    public static boolean connboo=false;
    Runnable SocketConnet =new Runnable() {
        @Override
        public void run() {
            try {
                String strfile= Environment.getExternalStorageDirectory()+"/Downs/";
                OHInterface.getG_server().setM_temppath(strfile);
               connboo= ModeRxjavaNet.Companion.getInstantMode().getOhInter().Init(null, mServerIp, (short) mSockPort, mConnType, callback);//建立连接
            }catch (Exception e){
                    Intent in=   new Intent();
                    in.setAction("SOCKCONN");
                    in.putExtra("comm","2");
                    context.sendBroadcast(in);
                sharedLoginActivity.setBoolearNetConn(false);
                Log.e("main","链接服务器失败"+e);
            }
            if (connboo){
                Log.e("main","链接服务器成功");
                Intent in=   new Intent();
                in.setAction("SOCKCONN");
                in.putExtra("comm","1");
                context.sendBroadcast(in);
                sharedLoginActivity.setBoolearNetConn(true);

            }


        }
    };

    /**
     * 启动通知
     * @param title         标题
     * @param content    内容
     * @param type 类型 0代表无声 1代表有声
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public Notification getNotification(String title,String content,int type,Context context,Integer account_id){
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.putExtra("main","1");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification notification = null;
        switch (type){
            case 0:
                 notification = new Notification.Builder(context)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentIntent(pendingIntent)
                        .build();
                break;
            case 1:
                 notification = new Notification.Builder(context)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setContentIntent(pendingIntent)
                         .setAutoCancel(true)
                        .build();
                break;
        }


        return notification;
    }

}
